#include "tools.cpp"

class Solution {
  public:
    void rotate(vector<vector<int>>& matrix) {
        if (matrix.empty()) return;
        int len = matrix.size() - 1, wid = matrix.front().size() - 1;

        std::function<std::pair<int, int>(int, int)> to_right = [&len, &wid](int level, int i) {
            return std::make_pair(i, wid - level);
        };

        std::function<std::pair<int, int>(int, int)> to_down = [&len, &wid](int level, int i) {
            return std::make_pair(wid - level, len - i);
        };

        std::function<std::pair<int, int>(int, int)> to_left = [&len, &wid](int level, int i) {
            return std::make_pair(wid - i, level);
        };

        // std::vector<std::pair<int, int>(int, int)> dir_cast{to_right, to_down, to_left};
        std::vector<decltype(to_down)> dir_cast{to_right, to_down, to_left};
        for (int level = 0; level < (matrix.size() - 1) / 2; ++level) {
            for (int dir = 0; dir < 3; ++dir) {
                for (int i = level; i < matrix.size() - level - 1; ++i) {
                    auto tmp = dir_cast[dir](level, i);
                    std::swap(matrix[level][i], matrix[tmp.first][tmp.second]);
                }
            }
        }
    }
};

int main() {
    Solution s;
    std::vector<std::vector<int>> matr{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    s.rotate(matr);
    return 0;
}
